package com.spsy.product.controller;

import com.alibaba.fastjson.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.product.domain.Product;
import com.spsy.product.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 沈昕希  &&  (=￣ω￣=)m
 * @Shen "再说错了又能怎么样呢？人生不就是由无数的错误组成的吗"
 * @xin "我是个无聊的人，我有无数的缺点，我也并不喜欢我自己，甚至厌恶我自己"
 * @date 2025/9/28-18:47
 */
@Slf4j
@RestController
@RequestMapping("/pro")
public class ProController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProductMapper productMapper;

    @PostMapping("/redisList")
    public R redisList(){
        String key = "redis_list";

        List<Product> list = productMapper.selectProductList(null);

        redisTemplate.opsForValue().set(key, JSON.toJSONString(list),30, TimeUnit.MINUTES);

        return R.ok(list);
    }

    @PostMapping("/getRedisById")
    public R getRedisById(Integer id){
        String key = "PRODUCT_KEY_PREFIX"+id;
        String s = redisTemplate.opsForValue().get(key);
        return R.ok(s);
    }

}
